package cn.lingyangwl.agile.msg.push.assembly;

import cn.lingyangwl.agile.msg.push.core.*;
import cn.lingyangwl.agile.msg.push.model.entity.*;
import cn.lingyangwl.agile.msg.push.model.rqrs.notice.*;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONReader;
import com.alibaba.fastjson2.JSONWriter;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
import org.mapstruct.Named;

/**
 * 消息设置
 *
 * @author shenguangyang
 */
@Mapper(componentModel = "spring")
public interface NoticeChannelAssembly {
  @Mappings({
      @Mapping(source = "settings", target = "settings", qualifiedByName = "parseMsgSettings")
  })
  NoticeChannelResp toNoticeChannelResp(NoticeChannel source);

  @Mappings({
      @Mapping(source = "settings", target = "settings", qualifiedByName = "msgSettingsToJson")
  })
  NoticeChannel toNoticeChannel(NoticeChannelSaveReq source);

  @Named("parseMsgSettings")
  default MsgSettings parse(String json) {
    return JSON.parseObject(json, MsgSettings.class, JSONReader.Feature.InitStringFieldAsEmpty);
  }

  @Named("msgSettingsToJson")
  default String toJson(MsgSettings obj) {
    return JSON.toJSONString(obj,  JSONWriter.Feature.WriteMapNullValue);
  }
}
